package com.laowang.framework.redis;


import com.alibaba.fastjson.JSONObject;
import com.laowang.common.utils.PayUtil;
import com.laowang.common.utils.StringUtils;

import com.laowang.project.pay.config.MyWxPayConfigUtil;
import com.laowang.project.system.controller.WxUserController;
import com.laowang.project.system.domain.*;

import com.laowang.project.system.mapper.WxOrderMapper;
import com.laowang.project.system.mapper.ZcWxOrderMapper;
import com.laowang.project.system.service.MyCyhRefundService;
import com.laowang.project.system.service.WxOrderService;
import com.laowang.project.system.service.WxUserService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
import oshi.jna.platform.mac.SystemB;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Slf4j
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {



    @Autowired
    private WxOrderMapper wxOrderMapper;

    @Autowired
    private WxUserService wxUserService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private MyCyhRefundService myCyhRefundService;

    @Autowired
    private ZcWxOrderMapper zcWxOrderMapper;


    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);

    }

    @Override
    public void onMessage(Message message, byte[] pattern) {

        String orderId = message.toString(); //ex
        log.debug(orderId);
        if (StringUtils.startsWith(orderId,"orderId:")){
            String after = StringUtils.substringAfter(orderId, ":");
            System.out.println("订单id:"+after);
            ZcWxOrder zcWxOrder = zcWxOrderMapper.selectZcWxOrderById(after);

            if (zcWxOrder !=null){
                if (zcWxOrder.getStatus().equals(0)){
                    zcWxOrder.setStatus(3);//'0待支付,1已支付,2已退款,3关闭支付,4已删除
                    zcWxOrderMapper.updateZcWxOrder(zcWxOrder);
                }

            }

        }
        if (StringUtils.startsWith(orderId,"zc:")){
            String after = StringUtils.substringAfter(orderId, ":");
            System.out.println("订单id:"+after);
            WxOrder wxOrder = wxOrderMapper.selectWxOrderById(after);

            if (wxOrder !=null){
                if (wxOrder.getStatus().equals(0)){
                    wxOrder.setStatus(3);//'0待支付,1已支付,2已退款,3关闭支付,4已删除
                    wxOrderMapper.updateWxOrder(wxOrder);
                }

            }

        }
        if (StringUtils.startsWith(orderId,"beifenxiang:")){
            //redisCache.setCacheObject("beifenxiang:"+wxUser.getMid(),"abc",30, TimeUnit.DAYS);
            String after = StringUtils.substringAfter(orderId, ":");
            WxUser wxUser = wxUserService.selectWxUserById(Integer.valueOf(after));
            if (wxUser !=null){

                if (wxUser.getVipLevel() != null && wxUser.getExpireTime() != null && wxUser.getExpireTime().after(new Date())) {

                } else {
                    //不是会员

                    if (wxUser.getPromoDor() != null && wxUser.getPromoDor().equals(1) && wxUser.getExpireTime1() != null && wxUser.getExpireTime1().after(new Date())) {

                    } else {
                        //不是推荐管

                        if (wxUser.getIsPartner() != null && wxUser.getIsPartner().equals(1) && wxUser.getExpireTime2() != null && wxUser.getExpireTime2().after(new Date())) {

                        } else {

                            //不是合伙人
                            if (wxUser.getStockHolder() != null && wxUser.getStockHolder().equals(1)) {

                            } else {
                                //不是股东
                                wxUser.setIntroMid(-1);
                                wxUserService.updateWxUser(wxUser);
                            }
                        }

                    }
                }

            }
        }

        if (StringUtils.startsWith(orderId,"tuikuan:")){
            String after = StringUtils.substringAfter(orderId, ":");
            MyCyhRefund myCyhRefund = myCyhRefundService.selectMyCyhRefundById(after);
            String info = myCyhRefund.getInfo();
            MyWxPayConfigUtil myWxPayConfigUtil = JSONObject.parseObject(info, MyWxPayConfigUtil.class);
            PayUtil.test5(myWxPayConfigUtil);
        }

    }
}
